package com.yanqu.road.server.cmd.activity.douluodalu;

import com.yanqu.road.entity.activity.douluodalu.DouLuoDaLuPatronsBattleEnemy;
import com.yanqu.road.logic.pb.DouLuoDaLuPb;
import com.yanqu.road.pb.activity.douluodalu.DouLuoDaLuProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.douluodalu.DouLuoDaLuModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.S_DOU_LUO_DA_LU_ONE_KEY_FIGHT_FROM_CROSS,desc = "一键挑战(自动打完剩余进度),跨服返回)")
public class DouLuoDaLuOneKeyFightCmdFromCross extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        DouLuoDaLuProto.DouLuoDaLuAutoBattleCrossRespMsg respMsg = DouLuoDaLuProto.DouLuoDaLuAutoBattleCrossRespMsg.parseFrom(packet.getBytes());
        if(respMsg.getRet() != 0){
            ret = respMsg.getRet();
            return;
        }
        DouLuoDaLuModule daLuModule = player.getModule(DouLuoDaLuModule.class);
        if(daLuModule.getUserData() == null || daLuModule.getUserData().getActivityId() != respMsg.getActivityId()){
            ret = GameErrorCode.E_GAME_ERROR;
            return;
        }

        List<DouLuoDaLuPatronsBattleEnemy> enemyList = new ArrayList<>();
        for (DouLuoDaLuProto.DouLuoDaLuPatronsBattleDataTemp battleDataTemp : respMsg.getEnemyPatronsList()) {
            enemyList.add(DouLuoDaLuPb.parseDouLuoDaLuPatronsBattleEnemy(battleDataTemp));
        }
        List<Integer> deathList = new ArrayList<>(respMsg.getDeathList());
        ret = daLuModule.oneKeyFight(respMsg.getAutoBuy(), respMsg.getBuyItem(), respMsg.getPercent(), deathList,
                DouLuoDaLuPb.parseDouLuoDaLuPatronsBattle(respMsg.getPatrons()), enemyList, respMsg.getThisTime());


    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_DOU_LUO_DA_LU_ONE_KEY_FIGHT, DouLuoDaLuProto.DouLuoDaLuAutoBattleRespMsg.newBuilder().setRet(ret));
        }
    }


}
